package com.smarttiger.zero.admin.service;

import com.smarttiger.zero.admin.dao.TaskDao;
import com.smarttiger.zero.admin.entity.TaskModel;
import com.smarttiger.zero.admin.task.QuartzScheduler;
import com.smarttiger.zero.admin.util.Constant;
import com.smarttiger.zero.admin.util.Message;
import com.smarttiger.zero.admin.util.ParamNames;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskService extends BaseService<TaskModel> {
    @Autowired
    TaskDao dao;

    @Autowired
    private QuartzScheduler quartzScheduler;

    @PostConstruct
    public void initMethod() {
        super.setDao(dao);
    }

    @ParamNames("name,page")
    public Message getTasks(String name,String page) {
        Message message = new Message();
        Pageable pageable = PageRequest.of(Integer.valueOf(page).intValue(), Constant.PAGESIZE, Sort.by(Sort.Direction.DESC,"createDate"));
        Page<TaskModel> all = dao.findAll(new Specification<TaskModel>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<TaskModel> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(name)) {
                    predicates.add(criteriaBuilder.like(root.get("name"), name + "%"));
                }
                criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createDate")));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        },pageable);
        long totalElements = all.getTotalElements();
        List<TaskModel> taskModels = all.getContent();
        message.setData(taskModels);
        message.setTotal((int)totalElements);
        return message;
    }


    @Override
    public void save(TaskModel u) {
        super.save(u);
    }

    @Override
    public void update(TaskModel u) {
        try {
            quartzScheduler.deleteJob(dao.findById(u.getId()).get().getCode());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        u.setStatus("S");
        super.update(u);

    }

    @Override
    public void delete(String id) {
        try {
            quartzScheduler.deleteJob(dao.findById(id).get().getCode());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        super.delete(id);
    }
}
